<?php

namespace app\admin\model\outhouse;

use think\Model;
use traits\model\SoftDelete;
use think\Log;

class Kcmx extends Model
{
    // 表名
    protected $name = 'kcmx';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'datetime';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'iType_text'
    ];
    
    protected $type = [
      'iQuantity' => 'quantity',
      'store_quantity' => 'quantity'
    ];
    
    public function getItypeList()
    {
        return [
            '1' => __('Itype 1'),
            '2' => __('Itype 2'),
            '3' => __('Itype 3'),
            '4' => __('Itype 4'),
            '5' => __('Itype 5'),
            '6' => __('Itype 6'),
            '7' => __('Itype 7'),
            '8' => __('Itype 8'),
            '9' => __('Itype 9'),
            '10' => __('Itype 10'),
            '11' => __('Itype 11'),
            '12' => __('Itype 12'),
            '14' => __('Itype 14'),
            '15' => __('Itype 15'),//销售出库
            '16' => __('Itype 16'),
            '22' => __('Itype 22'),
            '23' => __('Itype 23'),//金蝶同步
            '55' => __('Itype 55'),
            '88' => __('Itype 88'),
            '99' => __('Itype 99'),

             '998' => __('Itype 998'),
            '999' => __('Itype 999')
        ];
    }


    public function getItypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['iType']) ? $data['iType'] : '');
        $list = $this->getItypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }
    public static function addData($param){
        $data = [
            'code' => $param['order_code'],
            'code_date' => $param['order_date'],
            'iType' => $param['order_list'],
            'iQuantity' => $param['quantity'],
            'cInvCode' => $param['cInvCode'],
            'cFree1' => $param['cFree1'],
            'shelves_id' => $param['shelves_id'],
            'createtime'=>date('Y-m-d H:i:s'),
        ];
        self::insert($data);
    }

    /**
     * @param $code
     * @param $code_date
     * @param $quantity
     * @param $cInvCode
     * @param $cFree1
     * @param $shelves_change_id
     * @param $warehouse_id
     * @param $type  出入库类型
     * @param $createtime
     * @return void
     */
    public static function commonKcmxParams($code,$code_date,$quantity,$cInvCode,$cFree1,$shelves_change_id,$warehouse_id,$type,$remark='',$cBatch=''){
        $kcmxData = [
            'code' => $code,
            'code_date' => $code_date,
            'iType' => $type,
            'iQuantity' => $quantity,
            'cInvCode' => $cInvCode,
            'cFree1' => $cFree1,
            'cBatch' => $cBatch,
            'shelves_id' =>$shelves_change_id,
            'warehouse_id' => $warehouse_id,
            'createtime'=>date('Y-m-d H:i:s'),
            'remark'=>$remark
        ];
        //Log::write($kcmxData);
        self::insert($kcmxData);
    }
    
    //获得某段时间，某物料的出、入数量
    public static function getKcmxQtyByDuringDate($warehouse_id,$shelvesId = 0,$cInvCode,$startDate='',$endDate='',$cBatch=''){
        if(!$warehouse_id||!$cInvCode){
            exception("统计期间数量，参数错误");
        }
        
        $where = [
            'warehouse_id' => $warehouse_id,
            'cInvCode' => $cInvCode,
            'shelves_id' => $shelvesId,
            'cBatch' => $cBatch,
            'code' => ['not like','删%']//删除的日志不计入
        ];
        
        if($startDate){
            if (strtotime($startDate) == false) {
                exception("统计期间数量，开始日期无效");
            }
            if(!$endDate){
                $endDate = date('Y-m-d H:i:s',time());
            }
            if (strtotime($endDate) == false) {
                exception("统计期间数量，结束日期无效");
            }
            
            $where['createtime'] = ['between',[$startDate,$endDate]];
        }
        //'1增加  0减少',
        $inqty = self::where($where)->where('type',1)->sum('iQuantity');
        $outqty = self::where($where)->where('type',0)->sum('iQuantity');
        
        $digits = 4;
        $qty_format_arr = config('site.float_data_formatter');
        if(isset($qty_format_arr['digits'])){
            $digits = $qty_format_arr['digits'];
        }
        
        //$inqty = round($qty,$inqty);
        //$outqty = round($qty,$outqty);
        $sy = round($inqty-$outqty,$digits);
        
        return [
            'in' => $inqty,
            'out' => $outqty,
            'sy' => $sy
        ];
    }

}
